//
//  CommendSearchViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/1.
//  Copyright © 2016年 KX. All rights reserved.
#import "FreeTravelNetwork.h"
#import "CellOfCommendSearchViewController.h"
#import "CommendSearchViewController.h"
#import "FreeTravelMacro.h"
#import "ModelOfPostSearchResult.h"
#import "ModelOfUserSearchResult.h"
#import "ModelOfDiscountSearchResult.h"
#import "ModelOfDestinationSearchResult.h"
#import "SearchResultViewController.h"
@interface CommendSearchViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UITextField *searchTextField;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation CommendSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubViews];
    [self createCollectionView];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)createSubViews{
    self.searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(60, 20, SCREEN_WIDTH-120, 44)];
    self.searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.searchTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.searchTextField];
    UIButton *search = [UIButton buttonWithType:UIButtonTypeCustom];
    search.frame = CGRectMake(SCREEN_WIDTH-50, 20, 40, 44);
    [self.view addSubview:search];
    [search setTitle:@"搜索" forState:UIControlStateNormal];
    [search setTitleColor:COLORFORFREETRAVEL forState:UIControlStateNormal];
    [search addTarget:self action:@selector(goSearch) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(10, 20, 40, 44);
    [self.view addSubview:back];
    [back setTitle:@"取消" forState:UIControlStateNormal];
    [back setTitleColor:COLORFORFREETRAVEL forState:UIControlStateNormal];
    [back addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH-40)/3, 30);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[CellOfCommendSearchViewController class] forCellWithReuseIdentifier:@"CommendSearchViewController"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arrayOfHot.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CellOfCommendSearchViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommendSearchViewController" forIndexPath:indexPath];
    
    
    cell.hotLabel.text = self.arrayOfHot[indexPath.item];

        return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SearchResultViewController *vc = [[SearchResultViewController alloc]init];

    vc.search = self.arrayOfHot[indexPath.item];
    
    [self.navigationController pushViewController:vc animated:YES];

}
- (void)setArrayOfHot:(NSArray *)arrayOfHot{
    _arrayOfHot = arrayOfHot;
    [self.collectionView reloadData];
}
- (void)goSearch{
    SearchResultViewController *vc = [[SearchResultViewController alloc]init];
    vc.search = self.searchTextField.text;
    
    
    [self.navigationController pushViewController:vc animated:YES];
    

}
- (void)goBack{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
